local hx__chenglue = fk.CreateSkill {
  name = "hx__chenglue",
  tags = {Skill.Switch},
}


hx__chenglue:addEffect("active", {
  name = "hx__chenglue",
  anim_type = "switch",
  prompt = function (self, player)
    return player:getSwitchSkillState("hx__chenglue", false) == fk.SwitchYang and "#hx__chenglue-active:::1:2" or "#hx__chenglue-active:::2:1"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(hx__chenglue.name, Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local isYang = player:getSwitchSkillState(hx__chenglue.name, true) == fk.SwitchYang
    player:drawCards(isYang and 1 or 2, hx__chenglue.name)
    if player.dead then return end
    local num = isYang and 2 or 1
    local toDiscard = room:askToDiscard(player, {
      min_num = num,
      max_num = num,
      include_equip = true,
      skill_name = hx__chenglue.name,
      cancelable = false,
      prompt = "#hx__chenglue-discard:::"..num,
      skip = true,
    })
    if #toDiscard == 0 then return end
    for _, id in ipairs(toDiscard) do
      local suit = Fk:getCardById(id):getColorString()
      if suit ~= "nocolor" then
        room:addTableMarkIfNeed(player, "@hx__chenglue-phase", suit)
      end
    end
    room:throwCard(toDiscard, hx__chenglue.name, player, player)
  end,
})

hx__chenglue:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.contains(player:getTableMark("@hx__chenglue-phase"), data.card:getColorString())
  end,
  on_refresh = function(self, event, target, player, data)
    data.extraUse = true
  end,
})

hx__chenglue:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    return card and table.contains(player:getTableMark("@hx__chenglue-phase"), card:getColorString())
  end,
  bypass_distances = function(self, player, skill, card)
    return card and table.contains(player:getTableMark("@hx__chenglue-phase"), card:getColorString())
  end,
})

return hx__chenglue
